﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public class LoadFromFileExample : MonoBehaviour
{
    public string Path;
    public string PrefabName;
    public string AssetName;
    public int functionId;
    private void Start()
    {
        switch (functionId)
        {
            case 1:
                LoadABFromFile();
                break;
            case 2:
                LoadABFromMemory();
                break;
            case 3:
                StartCoroutine(LoadABFromMemoryAsync());
                break;
            case 4:
                StartCoroutine(LoadABFromWWW());
                break;
            case 5:
                StartCoroutine(LoadABFromUnityWebRequest());
                break;
            case 6:
                LoadCube2();
                break;
            default:
                break;
        }
    }

    private T LoadAssetFromAB<T>(AssetBundle ab) where T: Object
    { 
        return ab.LoadAsset<T>(AssetName);
    }


    //LoadTest 
    private void LoadCube2()
    {
        AssetBundle.LoadFromFile("AssetBundles/material");
        AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/cube2");
        GameObject cube2 = ab.LoadAsset<GameObject>("cube2");
        Instantiate(cube2);
    }


    //从本地文件加载
    private void LoadABFromFile()
    {
        AssetBundle ab = AssetBundle.LoadFromFile(Path);
        Instantiate(LoadAssetFromAB<GameObject>(ab));
    }

    //从内存加载
    private void LoadABFromMemory()
    {
        AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(Path));
        Instantiate(LoadAssetFromAB<GameObject>(ab));
    }
    //异步从内存加载
    private IEnumerator LoadABFromMemoryAsync()
    {
        Debug.Log("Start LoadABFromMemoryAsync");
        AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(Path));
        yield return request;
        AssetBundle ab = request.assetBundle;
        Instantiate(LoadAssetFromAB<GameObject>(ab));
    }

    //LoadFromCacheOrDownload
    private IEnumerator LoadABFromWWW()
    {
        //第三种加载AB的方式 WWW
        while (Caching.ready == false)
        {
            yield return null;
        }
        //WWW www = WWW.LoadFromCacheOrDownload(@"file:///D:\Code\UnityProject\ZuckerAssetBundleMrg\ZuckerAssetBundleMrg\AssetBundles/cube4", 1);
        WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundles/scene/wall.unity3d", 1);
        yield return www;
        if (string.IsNullOrEmpty(www.error) == false)
        {
            Debug.Log(www.error);
            yield break;
        }
        AssetBundle ab = www.assetBundle;
        Instantiate(LoadAssetFromAB<GameObject>(ab));
    }

    //LoadFromCacheOrDownload
    private IEnumerator LoadABFromUnityWebRequest()
    {
        //第四种方式，使用UnityWebRequest（目前可以取代WWW）
        string uri = @"file:///D:\Code\UnityProject\ZuckerAssetBundleMrg\ZuckerAssetBundleMrg\AssetBundles/cube4";
        UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri);
        yield return request.SendWebRequest();
        AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
        Instantiate(LoadAssetFromAB<GameObject>(ab));
    }
}